<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Controller_Auth
 *
 * Steuert Login und Logout, Useridentifikation
 *
 * @copyright m.reschke
 * @author m.reschke
 * @version $Id$
 */

class Controller_Auth extends Controller_Template_Website
{

    /**
     * Login
     */
    public function action_login()
    {
        if ($this->request->method() == Request::POST) {
            // Post Params und Auth Object holen
            $post = $this->request->post();
            $auth = Auth::instance();

            $validation = Validation::factory($post);
            $validation
                ->rule(True, 'not_empty')
                ->rule('email', 'email')
                ->rule('password', 'min_length', array(':value', '3'));


            if (isset($post['remember'])) {
                $remember = true;
            } else {
                $remember = false;
            }

            if ($validation->check()) {
                if ($auth->login($post['email'], $post['password'], $remember)) {
                    Message::set( Message::SUCCESS, 'Login successful!' );
                    $this->redirect('welcome/index');
                }
                Message::set( Message::ERROR, 'unkown user ? try again !' );
                $this->redirect('auth/login');
            } else {
                $errors = $validation->errors('authMessages');
                Message::set( Message::ERROR, $errors );
            }

        }

        $this->template->title = 'Login';
        $this->template->content = View::factory('auth/login');
    }

    /**
     * Logout
     */
    public function action_logout()
    {
        $auth = Auth::instance();

        if ($auth->logged_in()) {
            $auth->logout();
        }

        $this->redirect('auth/login');

        $this->template->title = 'Logout';
        $this->template->content = View::factory('auth/logout');
    }

    /**
     * Registrierung
     */
    public function action_register()
    {
        if ($this->request->method() == Request::POST) {

            $post = $this->request->post();

            $validation = Validation::factory($post);
            $validation
                ->rule(True, 'not_empty') // fuer alle Post Werte
                ->rule('email', 'email')
                ->rule('password', 'min_length', array(':value', '3'))
                ->rule('password2', 'matches', array(':validation', ':field', 'password'));

            if ($validation->check()) {
                $user           = ORM::factory('User');
                $user->email    = $post['email'];
                $user->username = $post['email'];
                $user->password = Auth::instance()->hash($post['password']);

                if ($user->check()) {
                    if ($user->unique('email', $user->email)) {
                        try {
                            $user->save();
                            $user->add('roles', ORM::factory('Role', array('name' => 'login')));
                        }
                        catch (ORM_Validation_Exception $e) {
                            $errors = $e->errors();
                            $this->response->body($errors);
                        }

                        Message::set( Message::SUCCESS, 'Registration successful!');

                        $this->redirect('auth/register');
                    } else {
                        Message::set( Message::ERROR, 'User already exist! Try a other name' );
                    }

                    $this->redirect('auth/register');
                }
            } else {
                Message::set( Message::ERROR, $validation->errors('authMessages') );
                $this->redirect('auth/register');
            }
        }

        $this->template->title = 'Register';
        $this->template->content = View::factory('auth/register');
    }

}